package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;
import android.support.annotation.LayoutRes;
import android.view.View;

import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/12/20.
 */

public class UserItemModel extends BindingListItem {
    private String nickName;
    private String avatar;
    private String job;
    private String label;
    private String company;
    private boolean attented;
    private XListModel parent;
    private boolean expert;
    private boolean allowUnAttetion;
    private int uid;

    @Bindable
    public boolean isAllowUnAttetion() {
        return allowUnAttetion;
    }

    public void setAllowUnAttetion(boolean allowUnAttetion) {
        this.allowUnAttetion = allowUnAttetion;
    }

    public XListModel getParent() {
        return parent;
    }

    public void setParent(XListModel parent) {
        this.parent = parent;
    }

    @Bindable
    public boolean isExpert() {
        return expert;
    }

    public void setExpert(boolean expert) {
        this.expert = expert;
        notifyPropertyChanged(BR.expert);
    }

    @Bindable
    public boolean isAttented() {
        return attented;
    }

    public void setAttented(boolean attented) {
        this.attented = attented;
        notifyPropertyChanged(BR.attented);
    }

    @Bindable
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
        notifyPropertyChanged(BR.nickName);
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
        notifyPropertyChanged(BR.avatar);
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
        notifyPropertyChanged(BR.job);
    }

    @Bindable
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
        notifyPropertyChanged(BR.label);
    }

    @Bindable
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
        notifyPropertyChanged(BR.company);
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    @Bindable
    public boolean isSelf() {
        return User.getCurrentUser().getUid() == uid;
    }

    public UserItemModel(Context context, @LayoutRes int layoutId) {
        super(context, layoutId);
    }

    public void onClickUnAttention(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.unAttention(getContext(), getUid())) {
                    setAttented(false);
                    if (parent != null) {
                        parent.notifyListChanged(ListBindingAdapter.ACTION_DEL, UserItemModel.this);
                    }
                }
            }
        }).start();
    }

    public void onClickAttention(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.addAttention(getContext(), getUid())) {
                    setAttented(true);
                    parent.notifyListChanged(ListBindingAdapter.ACTION_ADD, UserItemModel.this);
                }
            }
        }).start();
    }

    public void onClickItem(View view) {
        HomeActivity.showPersonalCentral(getContext(), getUid());
    }
}
